package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.framework.utils.HYCUtils;
import cn.huiyunche.base.service.interfaces.CUserBankcardService;
import cn.huiyunche.base.service.mappers.ext.CUserBankcardViewMapper;
import cn.huiyunche.base.service.model.CUserBankcardExample;
import cn.huiyunche.base.service.vo.CUserAdminVoBankcard;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CUserBankcardServiceImpl implements CUserBankcardService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CUserBankcardServiceImpl.class);

    @Autowired
    private CUserBankcardViewMapper cUserBankcardViewMapper = null;

    private CUserBankcardViewMapper getCUserBankcardViewMapper() {
        return this.cUserBankcardViewMapper;
    }

    @Override
    public Map<String, Object> getUserBackList(PageVo page) {
        Map<String, Object> map = new HashMap<>();
        try {
            CUserBankcardExample example = new CUserBankcardExample();
            example.setLimitStart(page.getStartIndex());
            example.setLimitEnd(page.getPageSize());
            if (StringUtils.isNoneEmpty(page.getOrder())) {
                example.setOrderByClause(page.getOrder());
            } else {
                example.setOrderByClause(" b.id DESC");
            }

            page.setTotalRecord(this.getCUserBankcardViewMapper().countByExample(example));
            List<CUserAdminVoBankcard> list = this.getCUserBankcardViewMapper().selectByExample(example);
            for (CUserAdminVoBankcard cUserAdminVoBankcard : list) {
                String code = cUserAdminVoBankcard.getCode();
                if (StringUtils.isNoneEmpty(code)) {
                    code = HYCUtils.decryptBankCard(code);
                    cUserAdminVoBankcard.setCode(code);
                }
            }

            map.put("page", page);
            map.put("usercards", list);
            return map;
        } catch (Exception e) {
            LOGGER.error("select user bank card list error : {}.", e);
            throw new BusinessException("查询银行卡列表异常");
        }
    }

/*	@Override
    public Map<String, Object> getUserBackList(PageVo page, CUserAdminVoBankcard bank, int userType) {
		LOGGER.info("select userBackList params page : {}.", page, userType);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("limitStart", page.getStartIndex());
			paramsMap.put("limitEnd", page.getPageSize());
			String orderByClause = StringUtils.isNotBlank(page.getOrder()) ? page.getOrder() : " u.id DESC";
			paramsMap.put("orderByClause", orderByClause);
			if(null != bank) {
				paramsMap.put("realName", bank.getRealName());
				paramsMap.put("phone", bank.getPhone());
				paramsMap.put("bankName", bank.getBankName());
			}
			
			page.setTotalRecord(this.getCUserBankcardViewMapper().countByConditions(paramsMap));
			List<CUserAdminVoBankcard> list = this.getCUserBankcardViewMapper().selectByConditions(paramsMap);
			for (CUserAdminVoBankcard cUserAdminVoBankcard : list) {
				String code = cUserAdminVoBankcard.getCode();
				if(StringUtils.isNoneEmpty(code)){
					code = HYCUtils.decryptBankCard(code);
					cUserAdminVoBankcard.setCode(code);
				}
			}
			
			map.put("page", page);
			map.put("usercards", list);
			return map;
		} catch (Exception e) {
			LOGGER.error("select user bank card list error : {}.", e);
			throw new BusinessException("查询银行卡列表异常");			
		}
	}*/
}
